package com.demo.android.endi.fragment;

import java.util.HashMap;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYValueSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

import com.demo.android.endi.R;

/**
 * A fragment showing the list of regions
 * @author siddhu.warrier
 *
 */
public class ChartFragment extends Fragment {
	/**
	 * Reference to RetrieveRegionModel object
	 */
	

	/** the available AWS regions */
	private HashMap<String, String> regionData;

	/** The connectivity data */
	private HashMap<String, String> connectionData;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    	return inflater.inflate(R.layout.dashboard_chart, container, false);
    }

	/**
	 * 
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		Intent intent = getActivity().getIntent();
		this.connectionData = (HashMap<String, String>) intent.getSerializableExtra("org.elasticdroid.LoginView.connectionData");
	}

	@Override
	public void onResume() {
		super.onResume();
		//check if we need to get region data.
		//if we have neither region data, nor a running retrieveregionmodel
		//execute retrieve region model
		/*if (regionData == null) {
			//execute retrieveregionmodel unless not executing already
			if (retrieveRegionTask == null) {
				retrieveRegionTask = new RetrieveRegionTask();
				retrieveRegionTask.execute(new HashMap<?,?>[]{connectionData});
			}
		}*/

		drawChart();
	}

	private void drawChart() {
		try{
			// Our first data
			int[] x = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // x values!
			int[] y =  { 30, 34, 45, 57, 77, 89, 100, 111 ,123 ,145 }; // y values!
			TimeSeries series = new TimeSeries("Line1"); 
			for( int i = 0; i < x.length; i++)
			{
				series.add(x[i], y[i]);
			}
			
			
			
			XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
			dataset.addSeries(series);
			
			
			XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer(); // Holds a collection of XYSeriesRenderer and customizes the graph
			XYSeriesRenderer renderer = new XYSeriesRenderer(); // This will be used to customize line 1
			
			mRenderer.addSeriesRenderer(renderer);
			
			
			// Customization time for line 1!
			renderer.setColor(Color.RED);
			renderer.setPointStyle(PointStyle.SQUARE);
			renderer.setFillPoints(true);
			
			
	
		    //renderer.setZoomEnabled(false, false);
	
		    GraphicalView chartView = ChartFactory.getLineChartView(getActivity(), dataset, mRenderer);
			/*if (chartView != null) {
			 * 
			chartView.repaint();	
			}
			else { */
			//	chartView = ChartFactory.getBubbleChartView(getActivity(), series, renderer);
			//}
			
	
		    LinearLayout layout = (LinearLayout) getActivity().findViewById(R.id.dashboard_chart_layout);	    
		    //layout.removeAllViews();
		    layout.addView(chartView, new LayoutParams(960,
		              LayoutParams.FILL_PARENT));
		}catch(Exception ex){
			System.out.println(ex.getMessage());
		}
		
	}

	public void cleanUpAfterTaskExecution() {
		// TODO Auto-generated method stub

	}
}